package com.jingze.plugins.monitor;

import com.getcapacitor.JSObject;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.jingze.plugins.monitor.customize.Check;
import com.jingze.plugins.monitor.customize.Run;

@NativePlugin
public class MonitorPlugin extends Plugin {

    @PluginMethod
    public void echo(PluginCall call) {
        String value = call.getString("value");

        JSObject ret = new JSObject();
        ret.put("value", value);
        call.success(ret);
    }


    /**
     * 声明一个可以被JavaScript调用的方法
     * @author zhongmb
     * @since 2020-12-25
     */
    @PluginMethod
    public void test(PluginCall call) {
        // 如果成功返回一个数字
        JSObject ret = new JSObject();
        ret.put("code", true);
        JSObject info = new JSObject();
        info.put("data", "数据成功调用，没有报错");
        ret.put("info", info);
        System.out.println("成功执行3");
        call.resolve(ret);
    }


    /**
     * 声明一个可以被JavaScript调用的方法
     * @author zhongmb
     * @since 2020-12-25
     */
    @PluginMethod
    public void doRun(PluginCall call) {
        System.out.println("---------------前端调用test方法-------------------");
        System.out.println("前端传参："  + call.getData());
        try {
            String json = call.getString("value");
            System.out.println("成功执行1");
            Run run = new Run();
            run.putRun(json);
            System.out.println("成功执行2");
            // 如果成功返回一个数字
            JSObject ret = new JSObject();
            ret.put("code", true);
            JSObject info = new JSObject();
            info.put("data", "数据成功调用，没有报错");
            ret.put("info", info);
            System.out.println("成功执行3");
            call.resolve(ret);
        } catch (InterruptedException e) {
            System.out.println("失败1");
            e.printStackTrace();
            call.reject(e.getLocalizedMessage(), e);
        } catch (Exception e) {
            System.out.println("失败2");
            e.printStackTrace();
            call.reject(e.getLocalizedMessage(), e);
        }
    }

    /**
     * 声明一个可以被JavaScript调用的方法
     * @author zhongmb
     * @since 2020-12-27
     */
    @PluginMethod
    public void check(PluginCall call) {
        System.out.println("---------------前端调用test方法-------------------");
        System.out.println("前端传参："  + call.getData());
        try {
            int lightNo = call.getInt("lightNo");
            int channel = call.getInt("channel");
            String address = call.getString("address");
            System.out.println("成功执行1");
            Check check = new Check();
            check.doCheck(address, channel, lightNo);
            System.out.println("成功执行2");
            // 如果成功返回一个数字
            JSObject ret = new JSObject();
            ret.put("code", true);
            JSObject info = new JSObject();
            info.put("data", "数据成功调用，没有报错");
            ret.put("info", info);
            System.out.println("成功执行3");
            call.resolve(ret);
        } catch (InterruptedException e) {
            System.out.println("失败1");
            e.printStackTrace();
            call.reject(e.getLocalizedMessage(), e);
        } catch (Exception e) {
            System.out.println("失败2");
            e.printStackTrace();
            call.reject(e.getLocalizedMessage(), e);
        }
    }


    /**
     * 声明一个可以被JavaScript调用的方法
     * @author zhongmb
     * @since 2020-12-27
     * @param -type 1-冷却液温度 2-放电 3-充电
     * @param -value 0-开|高 1-关|低
     */
    @PluginMethod
    public void perform(PluginCall call) {
        System.out.println("---------------前端调用test方法-------------------");
        System.out.println("前端传参："  + call.getData());
        try {
            String type = call.getString("type");
            String value = call.getString("value");
            System.out.println("成功执行1");
            Run run = new Run();
            String json = "";
            if (type == "1" && value == "1") {
                json = DataUtil.coolingFluid;
            } else {
                json = DataUtil.LightsOut();
            }
            run.putRun(json);
            System.out.println("成功执行2");
            // 如果成功返回一个数字
            JSObject ret = new JSObject();
            ret.put("code", true);
            JSObject info = new JSObject();
            info.put("data", "数据成功调用，没有报错");
            ret.put("info", info);
            System.out.println("成功执行3");
            call.resolve(ret);
        } catch (InterruptedException e) {
            System.out.println("失败1");
            e.printStackTrace();
            call.reject(e.getLocalizedMessage(), e);
        } catch (Exception e) {
            System.out.println("失败2");
            e.printStackTrace();
            call.reject(e.getLocalizedMessage(), e);
        }
    }
}
